﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Travianer.Common;
using Travianer.Controller;
using Travianer.Model;

namespace Travianer
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
            LoadForm();

            this.WindowState = FormWindowState.Normal;
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private void LoadForm()
        {
            this.Text = "登陆";
            txtServerLocation.Text = Configuration.ServerLocation;
            txtUserName.Text = Configuration.UserName;
            txtPassword.Text = Configuration.Password;
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            ResponseContainer ent;
            try
            {
                ent = HandleController.GetInstance().Login(txtServerLocation.Text.Trim(), txtUserName.Text.Trim(), txtPassword.Text.Trim());
                if (LogicController.GetInstance().IsShownew(ent.Url))
                {
                    ent = HandleController.GetInstance().Get(txtServerLocation.Text.Trim() + "village1.php?_gn_");
                }
                if (LogicController.GetInstance().IsLoginForm(ent.Url))
                {
                    MessageBox.Show("登录失败！");
                    return;
                }
            }
            catch
            {
                MessageBox.Show("连接失败！");
                return;
            }
            ApplicationHelper.strServerLocation = txtServerLocation.Text.Trim();
            ApplicationHelper.strUserName = txtUserName.Text.Trim();
            ApplicationHelper.strPassword = txtPassword.Text.Trim();
            Configuration.ServerLocation = txtServerLocation.Text.Trim();
            Configuration.UserName = txtUserName.Text.Trim();
            Configuration.Password = txtPassword.Text.Trim();
            MainForm mf = new MainForm(ent.SourceCode);
            mf.Text = txtUserName.Text.Trim() + "        " + txtServerLocation.Text.Trim();
            mf.Show();
            this.Visible = false;
        }
    }
}
